<template>
    <div class="change-branding">

        <div class="change-branding-item" v-for="item in rows" :key="item.type" :empty="item.empty">

            <div class="change-branding-title">
                <span v-if="item.type == 'tag'">Hang tag</span>
                <span v-if="item.type == 'pack'">Packaging bag</span>
            </div>

            <!-- 空 -->
            <div class="change-branding-empty" v-if="item.empty" @click="$emit('change',item.type)">
                <p></p>
            </div>

            <div class="change-branding-content" v-else>
                <BrandingThumbPreview :pieces.sync="item.design_list" :models.sync="item.model_img" :type="item.type" />

                <div class="change-branding-info">
                    <h3 v-text="item.template_name"></h3>
                    <p>
                        <strong>Designs</strong>
                        <UxPopper>
                            <span>Artwork</span>
                            <BrandingPiecesPreview slot="pop" :pieces="item.design_list" effect="popper" />
                        </UxPopper>
                    </p>
                    <p>
                        <strong>Price</strong>
                        <!-- <CommonCurrencyRate :price="item.total_price" type="unit|price" /> -->
                        <CommonLevelDiscount :level="userInfo.level" :price="item.total_price" type="unit|discount|icon" />

                    </p>
                    <fieldset>
                        <button @click="$emit('change',item.type)">Change</button>
                        <button @click="$emit('disconnect', item.type)">Unlink</button>
                    </fieldset>
                </div>
            </div>

        </div>

    </div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
export default {
    name: "ChangeBranding",
    props: {
        hangtag: { type: Object, default: null },
        packaging: { type: Object, default: null },
    },
    data () {
        return {
        }
    },
    computed: {
        ...mapGetters("user", {
            userInfo: "getUserInfo",
        }),

        rows () {

            const result = [
                { ...this.hangtag, type: 'tag', empty: !this.hangtag },
                { ...this.packaging, type: 'pack', empty: !this.packaging }
            ];

            result.map(item => {
                try {
                    const design = item.design_list || item.design;
                    if (Array.isArray(design)) {
                        item.design_list = design;
                    } else {
                        item.design_list = JSON.parse(design || '[]');
                    };
                } catch (error) {
                    console.log(error)
                }


                try {
                    if (Array.isArray(item.model_img)) {
                        item.model_img = item.model_img;
                    } else {
                        item.model_img = JSON.parse(item.model_img || '[]');

                    };
                } catch (error) {
                    console.log(error)
                }

            })

            return result;
        },

    },
}
</script>
<style lang="scss" scoped>
.change-branding {
    display: flex;
    flex-direction: row;
    margin: 0 -6px;
}

.change-branding-item {
    width: calc(50% - 12px);
    margin: 0 6px;
}

.change-branding-title {
    align-self: flex-start;
    font-size: 14px;
    font-family: Roboto-Medium, Roboto;
    font-weight: 500;
    color: #000000;
    line-height: 16px;
}

.change-branding-empty {
    position: relative;
    pointer-events: all;
    cursor: pointer;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    height: 320px;
    border-radius: 6px;
    box-sizing: border-box;
    margin-top: 12px;
    background: #f0f0f0;

    & > p {
        display: flex;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 140px;
        height: 140px;
        border-radius: 6px;
        border: 1px dashed #bdbdbd;
        align-items: center;
        justify-content: center;

        &::before {
            content: '';
            display: block;
            position: absolute;
            width: 48px;
            height: 6px;
            border-radius: 8px;
            background: #bdbdbd;
        }
        &::after {
            content: '';
            display: block;
            position: absolute;
            width: 6px;
            height: 48px;
            border-radius: 8px;
            background: #bdbdbd;
        }
    }

    &:hover {
        p {
            border-color: #43b9e2;
            &::after,
            &::before {
                background: #43b9e2;
            }
        }
    }
}

.change-branding-content {
    height: 320px;
    border-radius: 6px;
    box-sizing: border-box;
    margin-top: 12px;
    background: #f0f0f0;
    position: relative;
    display: flex;
    justify-content: center;
    padding: 32px 27px;

    .thumb-preview {
        width: 172px;
        height: 256px;
        padding-bottom: 0;
        background-color: #fff;
        overflow: hidden;
        position: relative;
        // border: 1px dashed #f44336;
        box-sizing: border-box;

        &::v-deep > figure {
            width: 256px;
            left: 50%;
            margin-left: -128px;
        }

        &[type='tag'] {
            background: none;

            &::v-deep img[type='background'] {
                display: none;
            }
        }
    }

    .change-branding-info {
        width: 138px;
        margin-left: 26px;
        display: flex;
        flex-direction: column;

        h3 {
            font-size: 14px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #000000;
            line-height: 22px;
            max-height: 44px;
            overflow: hidden;
            word-break: break-word;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            text-overflow: ellipsis;
            -webkit-line-clamp: 2;
            line-clamp: 2;
        }
        p {
            margin-top: 16px;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            strong {
                font-size: 12px;
                font-family: Roboto-Medium, Roboto;
                font-weight: 500;
                color: #000000;
                line-height: 16px;
            }
            .level-discount {
                margin-top: 10px;
                font-size: 12px;
                font-family: Roboto-Regular, Roboto;
                font-weight: 400;
                line-height: 14px;
            }

            .popper {
                margin-top: 6px;
                span {
                    text-decoration: underline;
                    &:hover {
                        color: #ea4c89;
                    }
                }
            }
        }

        fieldset {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: flex-end;

            button {
                width: 120px;
                height: 34px;
                background-color: #fff;
                font-size: 14px;

                & + button {
                    margin-top: 8px;
                }
            }
        }
    }
}
</style>
